﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLVMBAYDomain;

namespace QLVMBAY.UControl
{
    public partial class UcDatVe : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetNoiDiNoiDen();
                SetNgayThang();
                cboxNgayDi.SelectedValue = DateTime.Now.Day.ToString();
                cboxNgayDen.SelectedValue = DateTime.Now.Day.ToString();
                cboxThangDi.SelectedValue = DateTime.Now.Month.ToString() + "/" + DateTime.Now.Year.ToString();
                cboxThangDen.SelectedValue = DateTime.Now.Month.ToString() + "/" + DateTime.Now.Year.ToString();
                if (Session[ClientComm.KEY_TIM_VE] != null)
                {
                    var ttTim = (TTinTimVe)Session[ClientComm.KEY_TIM_VE];
                    rdKhuHoi.Checked = ttTim.IsKhuHoi;
                    rdMotChieu.Checked = !ttTim.IsKhuHoi;
                    ddlNoiDen.SelectedValue = ttTim.MaTPDen;
                    ddlNoiDi.SelectedValue = ttTim.MaTPDi;
                    cboxSLVeLon.SelectedValue = ttTim.SLVe.ToString();
                    cboxSLVeTEm.SelectedValue = ttTim.SLVeTrEm.ToString();
                    cboxNgayDi.SelectedValue = ttTim.ThoiGianDi.Day.ToString();
                    cboxThangDi.SelectedValue = ttTim.ThoiGianDi.Month + "/" + ttTim.ThoiGianDi.Year;
                    if (ttTim.IsKhuHoi)
                    {
                        cboxNgayDen.SelectedValue = ttTim.ThoiGianDen.Day.ToString();
                        cboxThangDen.SelectedValue = ttTim.ThoiGianDen.Month + "/" + ttTim.ThoiGianDen.Year;

                    }
                    lblNgayVe.Visible = cboxNgayDen.Visible = cboxThangDen.Visible =ttTim.IsKhuHoi == true;
                    
                }
            }
        }
        void AddNoi(DropDownListX dll, List<ThanhPho> lstTP)
        {
            dll.Items.Clear();
            dll.Items.Add(new ListItem("Chọn","0"));
            lstTP = lstTP.OrderByDescending(r => r.ItemQuocGia.IsMacDinh).ToList();
            var LstGroup = lstTP.Select(t => new { t.ItemQuocGia.TenQG, t.ItemQuocGia.MaQG, t.ItemQuocGia.IsMacDinh }).Distinct().OrderByDescending(r => r.IsMacDinh).ToList();
            foreach (var ob in LstGroup)
            {
                dll.AddItemGroup(ob.TenQG);
                var get = lstTP.Where(r => r.ItemQuocGia.MaQG == ob.MaQG).OrderByDescending(r => r.IsMacDinh).ToList();
                foreach (var obb in get)
                {
                    dll.Items.Add(new ListItem(obb.TenThanhPho + " (" + obb.MaThanhPho.Trim() + ")", obb.MaThanhPho));
                }
            }
        }
        void SetNoiDiNoiDen()
        {
            
            var lstTP = ClientComm.Ints.ThanhPho.GetAllTPForDatVe(true);            
            lstTP = lstTP.OrderByDescending(r => r.ItemQuocGia.IsMacDinh).ToList();
            AddNoi(ddlNoiDi, lstTP);
            AddNoi(ddlNoiDen, lstTP);          
            
        }
        void SetNgayThang()
        {
            ClientComm.Ints.LoadNgay(cboxNgayDen);
            ClientComm.Ints.LoadNgay(cboxNgayDi);
            ClientComm.Ints.LoadThang(cboxThangDen);
            ClientComm.Ints.LoadThang(cboxThangDi);
        }       

        protected void rdMotChieu_CheckedChanged(object sender, EventArgs e)
        {
            lblNgayVe.Visible = cboxNgayDen.Visible = cboxThangDen.Visible = !rdMotChieu.Checked == true;
        }

        protected void rdKhuHoi_CheckedChanged(object sender, EventArgs e)
        {
            lblNgayVe.Visible = cboxNgayDen.Visible = cboxThangDen.Visible = rdMotChieu.Checked == false;
        }

        protected void btnTimCBay_Click(object sender, EventArgs e)
        {
            if (ddlNoiDi.SelectedValue == "0")
            {
                Page.RegisterStartupScript("Err",
                                          "<script language='javascript'>alert('Vui lòng chọn nơi đi.');</script>");
                return;
            }
            if (rdKhuHoi.Checked == true)
            {
                if (ddlNoiDen.SelectedValue == "0")
                {
                    Page.RegisterStartupScript("Err",
                                               "<script language='javascript'>alert('Vui lòng chọn nơi đến.');</script>");
                    return;
                }
                if (ddlNoiDi.SelectedValue == ddlNoiDen.SelectedValue)
                {
                    Page.RegisterStartupScript("Err",
                                                                  "<script language='javascript'>alert('Vui lòng chọn lại nơi đến.');</script>");
                    return;
                }
            }
            var dtTimeDi =ClientComm.Ints.ToDate(cboxNgayDi.SelectedValue + "/" + cboxThangDi.SelectedValue);
            if (dtTimeDi < ClientComm.Ints.ToDate(DateTime.Now.Day+"/"+DateTime.Now.Month+"/"+DateTime.Now.Year))
            {
                Page.RegisterStartupScript("Err",
                                                         "<script language='javascript'>alert('Vui lòng lại chọn thời gian đi.');</script>");
                return;
            }
            var dtTimeDen = ClientComm.Ints.ToDate(cboxNgayDen.SelectedValue + "/" + cboxThangDen.SelectedValue);
            if (rdKhuHoi.Checked == true)
            {
                if (dtTimeDen < dtTimeDi)
                {
                    Page.RegisterStartupScript("Err",
                                                             "<script language='javascript'>alert('Vui lòng lại chọn thời gian đến.');</script>");
                    return;
                }
            }
            TTinTimVe obj = new TTinTimVe()
            {
                IsKhuHoi = rdKhuHoi.Checked == true,
                MaTPDen = ddlNoiDen.SelectedValue,
                TenTPDen = ddlNoiDen.SelectedItem.Text,
                MaTPDi = ddlNoiDi.SelectedValue,
                TenTPDi = ddlNoiDi.SelectedItem.Text,
                ThoiGianDi = dtTimeDi,
                ThoiGianDen = dtTimeDen,
                SLVe = int.Parse(cboxSLVeLon.SelectedValue),
                SLVeTrEm = int.Parse(cboxSLVeTEm.SelectedValue)
            };
            Session[ClientComm.KEY_TIM_VE] = obj;
            Response.Redirect("Pages/pTimChuyenBay.aspx");
        }

       
    }
}